#! /usr/bin/env/ python
# -*- encoding: utf-8 -*-

'''
autores: 
	Luis Franco 		nº 12052
	Ricardo Cabecinha	nº 12071

CPS 2012

Date: 06-02-2012

This program creates N pseudo-random numbers

Virtual Box - Virtual Machine: Linux Mint 11 2.6.38-8-generic

processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 23
model name	: Intel(R) Core(TM)2 Duo CPU     T6600  @ 2.20GHz
stepping	: 10
cpu MHz		: 2108.307
cache size	: 6144 KB
address sizes	: 36 bits physical, 48 bits virtual

note: the execution time have been taken including output time
output: N[k] = randomized_number

Execution times:
10		Numbers: Code time 0.0100 seconds
100		Numbers: Code time 0.0000 seconds
1000	Numbers: Code time 0.0300 seconds
10000	Numbers: Code time 0.2500 seconds
100000 	Numbers: Code time 2.1600 seconds
1000000	Numbers: Code time 22.4400 seconds

'''

import random
import time


'''
Function that creates a 1000 entry table
Each position of table stores a random between 0 and 9
every random number of table will occur the same number of times (100)
'''
def fillTable():
	table = [0 for x in range(0,1000)]	
	genCount = [0 for x in range (0,10)]
	it = 0

	while it < 1000:
		number =  random.randint(0,9)
		if (genCount[number] > 99):
			continue
		genCount[number] += 1
		table[it] = number
		it += 1
		pass
 
	return table

'''
Function that swaps table[p] and table[0]
receaves p position
'''
def swapVal(p):
	temp = table[0]
	table[0] = table[p]
	table[p] = temp



'''
Exception for user input invalid number
'''
while True:
	try:
		nIter = int(raw_input("Enter the number of Random numbers: "))
		break
	except ValueError:
		print "Oops! That was no valid number! Try again..."
	pass

t1 = time.clock()
'''
fills table
'''
table = fillTable()
'''
get an initial random number
'''
p = random.randint(0,999)
'''
iterator used only for printing
'''
n = 1 

'''
creates N random numbers defined by user
'''
while nIter > 0:
	#print "p = ", p
	#get z1
	if (p < 1000):
		z1 = table[p]
		swapVal(p)
	else:
		z1 = table[0]
		#swapVal(p)
	
		#get z2
	if (p < 999):
		z2 = table[p + 1]
		swapVal(p + 1)
	else:
		z2 = table[0]
		#swapVal(p+1)
		#get z3
	if (p < 998):
		z3 = table[p + 2]
		swapVal(p + 2)
				
	else:
		z3 = table[0]
		#swapVal(p + 2)
		
	#get z4
	if (p < 997):
		z4 = table[p + 3]
		swapVal(p + 3)
	else:
		z4 = table[0]
		#swapVal(p+3)

	'''
	nK 16-bit integer randomized number
	1st 4 bits = z1
	2nd 4 bits = z2
	3rd 4 bits = z3
	last 4 bits = z4
	'''
	nK = z1 << 12 | z2 << 8 | z3 << 4 | z4
	print 'N[{0}]= '.format(n), nK
	p = (z1 * 100 + z2 * 10 + z3)
	n +=1
	nIter -= 1
	pass

t2 = time.clock()


print 'Code time %.4f seconds' % (t2 - t1)


